<template>
  <div class="page">
    <div class="enterpriseInfoTab">
      <ul>
        <li>
          <a
            @click="$router.push('/dlyrl/oa/propaycontract/PropayContractList?id='+$route.query.id+'&types=0')
            "
          >
            付款
          </a>
        </li>
        <li class="defaultActive">
          <a
            @click="$router.push('/dlyrl/oa/oaprojectcontract/OaProjectContractList?id='+$route.query.id+'&types=0')"
          >
            收款
          </a>
        </li>
      </ul>
    </div>
      <el-form size="small" :inline="true" class="query-form" ref="searchForm" :model="searchForm" @keyup.enter.native="refreshList()" @submit.native.prevent>
            <!-- 搜索框-->
         <el-form-item prop="peopleDepartment.id">
            <SelectTree
                  ref="peopleDepartment.id"
                  :props="{
                      value: 'id',             // ID字段名
                      label: 'name',         // 显示名称
                      children: 'children'    // 子级字段名
                    }"
                  size="small" 
                  placeholder="请选择所属部门"
                  :url="`/sys/office/treeData?type=2`"
                  :value="searchForm.peopleDepartment.id"
                  :clearable="true"
                  :accordion="true"
                  @getValue="(value) => {searchForm.peopleDepartment.id=value}"/>
         </el-form-item>
         <el-form-item prop="partyA">
                <el-input size="small" v-model="searchForm.partyA" placeholder="甲方" clearable></el-input>
         </el-form-item>
         <el-form-item prop="contractNumber">
                <el-input size="small" v-model="searchForm.contractNumber" placeholder="合同编号" clearable></el-input>
         </el-form-item>
         <el-form-item prop="contractName">
                <el-input size="small" v-model="searchForm.contractName" placeholder="合同名称" clearable></el-input>
         </el-form-item>
         <el-form-item prop="year">
                <el-date-picker
                      v-model="searchForm.year"
                      type="year"
                      value-format="yyyy"
                      format="yyyy"
                      style="width: 100%;"
                      @change="timeChange"
                      :clearable="false"
                      placeholder="签约时间/年">
                </el-date-picker>
         </el-form-item>
         <el-form-item prop="month">
                <el-date-picker
                  v-model="searchForm.month"
                  type="month"
                  value-format="MM"
                  format="MM"
                  @change="timeChange"
                  style="width: 100%;"
                  :clearable="false"
                  placeholder="签约时间/月">
                </el-date-picker>
         </el-form-item>
         <!-- <el-form-item prop="aPerson">
                <el-input size="small" v-model="searchForm.aPerson" placeholder="甲方负责人" clearable></el-input>
         </el-form-item>
         <el-form-item prop="aOffice">
                <el-input size="small" v-model="searchForm.aOffice" placeholder="甲方公司" clearable></el-input>
         </el-form-item>
         <el-form-item prop="bPerson.id">
            <user-select :limit='1' size="small" placeholder="请选择乙方负责人" :value="searchForm.bPerson.id" @getValue='(value) => {searchForm.bPerson.id=value}'></user-select>
         </el-form-item>
         <el-form-item prop="bOffice">
                <el-input size="small" v-model="searchForm.bOffice" placeholder="乙方公司" clearable></el-input>
         </el-form-item>
         <el-form-item prop="procInsId">
                <el-input size="small" v-model="searchForm.procInsId" placeholder="流程实例id" clearable></el-input>
         </el-form-item> -->
          <el-form-item style="float: right">
            <el-button type="primary" @click="refreshList()" size="small">查询</el-button>
            <el-button @click="resetSearch()" size="small">重置</el-button>
          </el-form-item>
      </el-form>
        <!-- 导入导出-->
        <el-dialog  title="导入Excel" :visible.sync="isImportCollapse">
          <el-form :inline="true" v-show="isImportCollapse"  class="query-form" ref="importForm">
             <el-form-item>
              <el-button type="default" @click="downloadTpl()" size="small">下载模板</el-button>
             </el-form-item>
             <el-form-item prop="loginName">
                <el-upload
                  class="upload-demo"
                  :action="`${this.$http.BASE_URL}/dlyrl/oa/oaprojectcontract/oaProjectContract/import`"
                  :on-success="uploadSuccess"
                   :show-file-list="true">
                  <el-button size="small" type="primary">点击上传</el-button>
                  <div slot="tip" class="el-upload__tip">只允许导入“xls”或“xlsx”格式文件！</div>
                </el-upload>
            </el-form-item>
          </el-form>
      </el-dialog>
      <div class="bg-white top">
      <el-row>
         <el-tabs v-model="activeName"  @tab-click="handleClick"  v-if="this.$route.query.types==0">
            <el-tab-pane label="实施中" name="0"></el-tab-pane>
            <el-tab-pane label="已完成" name="1"></el-tab-pane>
            <el-tab-pane label="未实施" name="2"></el-tab-pane>
          </el-tabs> 
        <!-- <el-button v-if="hasPermission('dlyrl:oa:oaprojectcontract:oaProjectContract:add')" type="primary" size="small" icon="el-icon-plus" @click="add()">新建</el-button> -->
        <!-- <el-button v-if="hasPermission('dlyrl:oa:oaprojectcontract:oaProjectContract:edit')" type="warning" size="small" icon="el-icon-edit-outline" @click="edit()"
         :disabled="dataListSelections.length != 1" plain>修改</el-button> -->
        <!-- <el-button v-if="hasPermission('dlyrl:oa:oaprojectcontract:oaProjectContract:del')" type="danger"   size="small" icon="el-icon-delete" @click="del()"
                  :disabled="dataListSelections.length <= 0" plain>删除
        </el-button> -->
        <!-- <el-button-group class="pull-right">
            <el-button v-if="hasPermission('dlyrl:oa:oaprojectcontract:oaProjectContract:import')" type="default" size="small" icon="el-icon-upload2" title="导入" @click="isImportCollapse = !isImportCollapse, isSearchCollapse=false"></el-button>
            <el-button v-if="hasPermission('dlyrl:oa:oaprojectcontract:oaProjectContract:export')" type="default" size="small" icon="el-icon-download" title="导出" @click="exportExcel()"></el-button>
            <el-button
              type="default"
              size="small"
              icon="el-icon-refresh"
              @click="refreshList">
            </el-button>
        </el-button-group> -->
      </el-row>
    <el-table
      :data="dataList"
      :cell-style="cellStyles"
      :row-class-name="tableRowClassName"
      :header-cell-class-name="cellHeadStyle"
      style="width:100%; border:1px solid #d6cfe2;"
      @selection-change="selectionChangeHandle"
      @sort-change="sortChangeHandle"
      v-loading="loading"
      size="small"
      show-summary
      height="calc(100% - 80px)"
      @expand-change="detail"
      class="table">
      <!-- <el-table-column
        type="selection"
        header-align="center"
        align="center"
        width="50">
      </el-table-column> -->
      <!-- <el-table-column type="expand">
      <template slot-scope="scope">
      <el-tabs>
            <el-tab-pane label="回款信息">
                  <el-table
                  size="small"
                  :cell-style="cellStyle"
                  :row-class-name="tableRowClassName"
                  :header-cell-class-name="cellHeadStyle"
                  style="width:100%; border:1px solid #d6cfe2;"
                  :data="scope.row.paymentInformationList">
                <el-table-column
                    prop="date"
                    show-overflow-tooltip
                    align="center"
                    label="日期">
                  </el-table-column>
                <el-table-column
                    prop="money"
                    show-overflow-tooltip
                    align="center"
                    label="金额">
                  </el-table-column>
                <el-table-column
                    prop="capitalizedAmount"
                    align="center"
                    show-overflow-tooltip
                    label="大写金额">
                  </el-table-column>
                  <el-table-column
                    prop="confirmor.name"
                    align="center"
                    label="确认人">
                  </el-table-column>
                <el-table-column
                    prop="remarks"
                    show-overflow-tooltip
                    align="center"
                    label="备注信息">
                  </el-table-column>
                </el-table>
              </el-tab-pane>
            </el-tabs>
      </template>
      </el-table-column> -->
      <!-- <el-table-column
        prop="people.name"
        show-overflow-tooltip
        align="center"
        label="申请人">
            <template slot-scope="scope">
              <el-link  type="primary" :underline="false" v-if="hasPermission('dlyrl:oa:oaprojectcontract:oaProjectContract:edit')" @click="edit(scope.row.id)">{{scope.row.people.name}}</el-link>
              <el-link  type="primary" :underline="false" v-else-if="hasPermission('dlyrl:oa:oaprojectcontract:oaProjectContract:view')"  @click="view(scope.row.id)">{{scope.row.people.name}}</el-link>
              <span v-else>{{scope.row.people.name}}</span>
            </template>
      </el-table-column>
      <el-table-column
        prop="peopleOffice.name"
        show-overflow-tooltip
        align="center"
        label="所属公司">
      </el-table-column>
      <el-table-column
        prop="peopleDepartment.name"
        show-overflow-tooltip
        align="center"
        label="所属部门">
      </el-table-column>
    <el-table-column
        prop="date"
        show-overflow-tooltip
        align="center"
        label="申请时间">
      </el-table-column> -->
    <el-table-column
        prop="contractNumber"
        show-overflow-tooltip
        align="center"
        fixed="left"
        label="合同编号">
        <!-- <template slot-scope="scope">
              <el-link  type="primary" :underline="false"  v-if="hasPermission('dlyrl:oa:oaprojectcontract:oaProjectContract:view')" @click="edit(scope.row.id)">{{scope.row.contractNumber}}</el-link>
              <el-link  type="primary" :underline="false" v-else-if="hasPermission('dlyrl:oa:oaprojectcontract:oaProjectContract:edit')"  @click="view(scope.row.id)">{{scope.row.contractNumber}}</el-link>
              <span v-else>{{scope.row.contractNumber}}</span>
        </template> -->
        <template slot-scope="scope">
              <el-link  type="primary" :underline="false"   @click="view(scope.row.id)">{{scope.row.contractNumber}}</el-link>
        </template>
      </el-table-column>
    <el-table-column
        prop="contractName"
        show-overflow-tooltip
        align="center"
        label="合同名称">
      </el-table-column>
    <el-table-column
        prop="partyA"
        show-overflow-tooltip
        align="center"
        label="甲方">
      </el-table-column>
      <el-table-column
        prop="partyB.name"
        show-overflow-tooltip
        align="center"
        label="乙方">
      </el-table-column>
      <el-table-column
        prop="bPerson.office.name"
        show-overflow-tooltip
        align="center"
        width="100"
        label="归属部门">
      </el-table-column>
    <el-table-column
        prop="contractPerson.name"
        show-overflow-tooltip
        align="center"
        width="120"
        label="部门负责人">
      </el-table-column>
    <el-table-column
        prop="contractAmount"
        show-overflow-tooltip
        align="center"
        width="120"
        label="合同总金额(元)">
      </el-table-column>
    <!-- <el-table-column
        prop="capitalizeContract"
        show-overflow-tooltip
        align="center"
        label="大写金额">
      </el-table-column> -->
    <el-table-column
        prop="refundAmount"
        show-overflow-tooltip
        align="center"
        width="120"
        label="回款金额(元)">
      </el-table-column>
    <!-- <el-table-column
        prop="returnCapital"
        show-overflow-tooltip
        align="center"
        label="大写金额">
      </el-table-column> -->
    <el-table-column
        prop="receiptRatio"
        show-overflow-tooltip
        align="center"
        width="120"
        label="回款比例(%)">
      </el-table-column>
      <!-- <el-table-column
        prop="contractPerson.name"
        show-overflow-tooltip
        align="center"
        label="合同负责人">
      </el-table-column> -->
    <el-table-column
        prop="unpaidAmount"
        show-overflow-tooltip
        align="center"
        width="120"
        label="未回款金额(元)">
      </el-table-column>
    <el-table-column
        prop="unpaidRatio"
        show-overflow-tooltip
        align="center"
        width="120"
        label="未回款比例(%)">
      </el-table-column>
    <!-- <el-table-column
        prop="capitalizedAmount"
        show-overflow-tooltip
        align="center"
        label="大写金额">
      </el-table-column> -->
    <!-- <el-table-column
        prop="aPerson"
        show-overflow-tooltip
        align="center"
        label="甲方负责人">
      </el-table-column> -->
    <!-- <el-table-column
        prop="aPhone"
        show-overflow-tooltip
        align="center"
        label="甲方电话">
      </el-table-column> -->
    <!-- <el-table-column
        prop="aOffice"
        show-overflow-tooltip
        align="center"
        label="甲方公司">
      </el-table-column>
    <el-table-column
        prop="aAddress"
        show-overflow-tooltip
        align="center"
        label="甲方地址">
      </el-table-column>
    <el-table-column
        prop="aBillingName"
        show-overflow-tooltip
        align="center"
        label="甲方开票名称">
      </el-table-column>
    <el-table-column
        prop="aBillingNumber"
        show-overflow-tooltip
        align="center"
        label="甲方开票纳税人识别号">
      </el-table-column>
    <el-table-column
        prop="aBillingAddress"
        show-overflow-tooltip
        align="center"
        label="甲方开票地址">
      </el-table-column>
    <el-table-column
        prop="aBillingPhone"
        show-overflow-tooltip
        align="center"
        label="甲方开票电话">
      </el-table-column>
    <el-table-column
        prop="aBillingBank"
        show-overflow-tooltip
        align="center"
        label="甲方开票开户行">
      </el-table-column>
    <el-table-column
        prop="aAccountNumber"
        show-overflow-tooltip
        align="center"
        label="甲方开票账号">
      </el-table-column> -->
      <!-- <el-table-column
        prop="bPerson.name"
        show-overflow-tooltip
        align="center"
        label="乙方负责人">
      </el-table-column> -->
    <!-- <el-table-column
        prop="bPhone"
        show-overflow-tooltip
        align="center"
        label="乙方电话">
      </el-table-column> -->
    <!-- <el-table-column
        prop="bOffice"
        show-overflow-tooltip
        align="center"
        label="乙方公司">
      </el-table-column>
    <el-table-column
        prop="bAddress"
        show-overflow-tooltip
        align="center"
        label="乙方地址">
      </el-table-column>
    <el-table-column
        prop="bBillingName"
        show-overflow-tooltip
        align="center"
        label="乙方开票名称">
      </el-table-column>
    <el-table-column
        prop="bBillingNumber"
        show-overflow-tooltip
        align="center"
        label="乙方开票纳税人识别号">
      </el-table-column>
    <el-table-column
        prop="bBillingAddress"
        show-overflow-tooltip
        align="center"
        label="乙方开票地址">
      </el-table-column>
    <el-table-column
        prop="bBillingPhone"
        show-overflow-tooltip
        align="center"
        label="乙方开票电话">
      </el-table-column>
    <el-table-column
        prop="bBillingBank"
        show-overflow-tooltip
        align="center"
        label="乙方开票开户行">
      </el-table-column>
    <el-table-column
        prop="bAccountNumber"
        show-overflow-tooltip
        align="center"
        label="乙方开票账号">
      </el-table-column> -->
    <!-- <el-table-column
        prop="remarks"
        show-overflow-tooltip
        align="center"
        label="备注信息">
      </el-table-column> -->
      <el-table-column
        prop="earlywarning"
        show-overflow-tooltip
        align="center"
        width="120"
        label="合同有效期">
      </el-table-column>
      <el-table-column
        header-align="center"
        align="center"
        fixed="right"
        :key="Math.random()"
        width="70"
        label="操作">
        <template  slot-scope="scope">
          <!-- <el-button v-if="hasPermission('dlyrl:oa:oaprojectcontract:oaProjectContract:view')" type="text" icon="el-icon-view" size="mini" @click="view(scope.row.id)">查看</el-button> -->
          <el-button v-if="hasPermission('dlyrl:oa:oaprojectcontract:oaProjectContract:edit')" type="text" icon="el-icon-edit" size="mini" @click="edit(scope.row.id)">修改</el-button>
          <!-- <el-button v-if="hasPermission('dlyrl:oa:oaprojectcontract:oaProjectContract:del')" type="text"  icon="el-icon-delete" size="small" @click="del(scope.row.id)">删除</el-button> -->
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
      :current-page="pageNo"
      :page-sizes="[10, 20, 50, 100]"
      :page-size="pageSize"
      :total="total"
      background
      layout="total, sizes, prev, pager, next, jumper">
    </el-pagination>
    </div>
    <!-- 弹窗, 新增 / 修改 -->
    <el-dialog :title="title"   size="small" width="70%" :visible.sync="visible"> 
      <OaProjectContractForm  ref="oaProjectContractForm" @refreshDataList="refreshList"></OaProjectContractForm>
      <span slot="footer" class="dialog-footer">
        <el-button size="small"  @click="closeDialog">关闭</el-button>
        <el-button size="small" type="primary"  @click="save">确定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
  import OaProjectContractForm from './OaProjectContractForm'
  import UserSelect from '@/components/userSelect'
  import SelectTree from '@/components/treeSelect/treeSelect.vue'
  import tableStyles from '../../../../../utils/mixins'
  export default {
    mixins: [tableStyles],
    data () {
      return {
        activeName:'0',//默认进行中
        searchForm: {
          peopleDepartment: {
            id: ''
          },
          partyA: '',
          contractNumber: '',
          contractName: '',
          aPerson: '',
          aOffice: '',
          bPerson: {
            id: ''
          },
          bOffice: '',
          procInsId: '',
          year:'',
          month:''
        },
        dataList: [],
        pageNo: 1,
        pageSize: 10,
        total: 0,
        orderBy: '',
        dataListSelections: [],
        isImportCollapse: false,
        loading: false,
        visible:false,
        title:'',
        times:'',//查询接口时间
        showSummary: false,
      }
    },
    components: {
      UserSelect,
      SelectTree,
      OaProjectContractForm
    },
    activated () {
      this.refreshList()
    },
     watch: {
      //监听相同路由下参数变化的时候，从而实现异步刷新
      '$route'(to,from){
        if(this.$route.query.types=='1'){
          this.activeName='3';
          this.refreshList();
        }else if(this.$route.query.types=='0'){
          this.activeName='0';
          this.refreshList();
        } 
      }
    },
    methods: {
      handleClick(){
        this.$refs.searchForm.resetFields();
        this.refreshList();
      },
     //合同预警
			cellStyles({
				row,
				column,
				rowIndex,
				columnIndex
			}) {
				if(row.earlywarning>0&&row.earlywarning<10){
					return "background-color:#CC3332;padding:0.15rem;border-left:1px solid #d6cfe2;";
				}else if(row.earlywarning>=10&&row.earlywarning<20){
					return "background-color:#FF6601;padding:0.15rem;border-left:1px solid #d6cfe2;";
				}else if(row.earlywarning>=20&&row.earlywarning<30){
					return "background-color:#FDFC09;padding:0.15rem;border-left:1px solid #d6cfe2;";
				}else if(row.earlywarning=='已过期'){
					return "background-color:#C1C1C1;padding:0.15rem;border-left:1px solid #d6cfe2;";
					
				}else {
					return "padding:0.15rem;border-left:1px solid #d6cfe2;border-top:1px solid #d6cfe2";
				}
			},
      closeDialog(){
        this.visible=false;
        this.$refs.oaProjectContractForm.signatureVal='';
      },
      //父组件调用子组件方法
      save() {
        this.$refs.oaProjectContractForm.submit();
        this.$refs.oaProjectContractForm.$refs['inputForm'].validate((valid) => {
          if(valid){         
            this.visible=false;
          }      
        })
        setTimeout(this.refreshList,1200)
      },
      timeChange(){
        console.log(this.searchForm.month,'m')
        if(this.searchForm.year=='' && this.searchForm.month!=''){
          var date=new Date;
          var y = date.getFullYear()
          this.times=y+'-'+this.searchForm.month;
        }else if(this.searchForm.year!='' && this.searchForm.month==''){
           this.times=this.searchForm.year;
        }else if(this.searchForm.year!='' && this.searchForm.month!='' ){
            this.times=this.searchForm.year+'-'+this.searchForm.month
        }else if(this.searchForm.year=='' && this.searchForm.month=='' ){
            this.times=''
        }
      },
      // 获取数据列表
      refreshList () {
        if(this.$route.query.types=='1'){
          this.activeName='3';
        }
        this.loading = true
        this.$http({
          url: '/dlyrl/oa/oaprojectcontract/oaProjectContract/list',
          method: 'get',
          params: {
            'pageNo': this.pageNo,
            'pageSize': this.pageSize,
            'orderBy': this.orderBy,
            'peopleDepartment ':this.searchForm.peopleDepartment.id,
            'contractNumber':this.searchForm.contractNumber,
            'contractName':this.searchForm.contractName,
            'partyA':this.searchForm.partyA,
            'signingTime':this.times,
            'type':this.activeName,
            'peopleOffice':this.$route.query.id
          }
        }).then(({data}) => {
          if (data && data.success) {
            this.dataList = data.page.list
            this.total = data.page.count
            this.loading = false
          }
        })
      },
      // 每页数
      sizeChangeHandle (val) {
        this.pageSize = val
        this.pageNo = 1
        this.refreshList()
      },
      // 当前页
      currentChangeHandle (val) {
        this.pageNo = val
        this.refreshList()
      },
      // 多选
      selectionChangeHandle (val) {
        this.dataListSelections = val
      },

    // 排序
      sortChangeHandle (obj) {
        if (obj.order === 'ascending') {
          this.orderBy = obj.prop + ' asc'
        } else if (obj.order === 'descending') {
          this.orderBy = obj.prop + ' desc'
        } else {
          this.orderBy = ''
        }
        this.refreshList()
      },
      // 新增
      // add () {
      //   this.$refs.oaProjectContractForm.init('add', '')
      // },
      // 修改
      edit (id) {
        this.visible=true;
        this.title='合同详情'
        id = id || this.dataListSelections.map(item => {
          return item.id
        })[0]
        this.$nextTick(() => {
          this.$refs.oaProjectContractForm.init(id, 'edit')
        }) 
      },
      // 查看
      view (id) {
        this.visible=true;
        this.title='合同详情'
        this.$nextTick(() => {
          this.$refs.oaProjectContractForm.init(id, 'view')
        }) 
      },
      // 删除
      // del (id) {
      //   let ids = id || this.dataListSelections.map(item => {
      //     return item.id
      //   }).join(',')
      //   this.$confirm(`确定删除所选项吗?`, '提示', {
      //     confirmButtonText: '确定',
      //     cancelButtonText: '取消',
      //     type: 'warning'
      //   }).then(() => {
      //     this.loading = true
      //     this.$http({
      //       url: '/dlyrl/oa/oaprojectcontract/oaProjectContract/delete',
      //       method: 'delete',
      //       params: {'ids': ids}
      //     }).then(({data}) => {
      //       if (data && data.success) {
      //         this.$message.success(data.msg)
      //         this.refreshList()
      //       }
      //       this.loading = false
      //     })
      //   })
      // },
     // 查看详情
      detail (row) {
        this.$http.get(`/dlyrl/oa/oaprojectcontract/oaProjectContract/queryById?id=${row.id}`).then(({data}) => {
          this.dataList.forEach((item, index) => {
            if (item.id === row.id) {
              item.paymentInformationList = data.oaProjectContract.paymentInformationList
            }
          })
        })
      },
      // 导入成功
      uploadSuccess (res, file) {
        if (res.success) {
          this.$message.success({dangerouslyUseHTMLString: true,
            message: res.msg})
        } else {
          this.$message.error(res.msg)
        }
      },
      // 下载模板
      downloadTpl () {
        this.$utils.download('/dlyrl/oa/oaprojectcontract/oaProjectContract/import/template')
      },
      exportExcel () {
        let params = {
          ...this.searchForm
        }
        this.$utils.download('/dlyrl/oa/oaprojectcontract/oaProjectContract/export', params)
      },
      resetSearch () {
        this.$refs.searchForm.resetFields()
        this.refreshList()
      }
    }
  }
</script>
<style scoped>
ul {
  margin: 0;
}
</style>